using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Web.UI;
using System.IO;

namespace PhotoFieldControl
{
    public class PhotoFieldControl : BaseFieldControl
    {        
        protected DropDownList drpdwnLists;
        protected Image imgMedewerker;
       // protected string selectedMedewerker;



        protected override string DefaultTemplateName
        {
            get
            {
                return "PhotoFieldRenderingTemplate";
            }
        }

        public override object Value
        {
            get
            {
                EnsureChildControls();
                Microsoft.SharePoint.SPFieldMultiColumnValue photoValue = new Microsoft.SharePoint.SPFieldMultiColumnValue(3);
                photoValue[0] = drpdwnLists.SelectedValue;
                photoValue[1] = drpdwnLists.SelectedItem.ToString();
                
               // selectedMedewerker =  +";#" + ;

                return photoValue;
            }
            set
            {
                EnsureChildControls();
                Microsoft.SharePoint.SPFieldMultiColumnValue photoValue = value as Microsoft.SharePoint.SPFieldMultiColumnValue;

                //selectedMedewerker = this.ItemFieldValue as string;
                drpdwnLists.SelectedValue = photoValue[0];
                //drpdwnLists.SelectedValue = selectedMedewerker;
            }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            if (Field != null)
            {
                switch (ControlMode)
                {
                    case SPControlMode.Display:

                        break;


                    default:
                        AssociateControlsFromUserControl(TemplateContainer);
                        break;

                }
            }
        }

        private void AssociateControlsFromUserControl(TemplateContainer usercontrol)
        {

            drpdwnLists = usercontrol.FindControl("drpdwnLists") as DropDownList;
            drpdwnLists.CssClass = this.CssClass;

            bool useThisWeb = (bool)Field.GetCustomProperty("UseThisSite");
            string webSiteUrl = Field.GetCustomProperty("WebSiteUrl").ToString();
            string listName = Field.GetCustomProperty("ListName").ToString();

            SPWeb web = null;

            if (useThisWeb)
            {
                web = SPContext.Current.Web;
            }
            else
            {
                try
                {
                    SPSite site = new SPSite(webSiteUrl);
                    if (site != null)
                        web = site.OpenWeb();
                }
                catch (FileNotFoundException)
                {
                    web = null;
                }
            }

            drpdwnLists.Items.Add("None");

            if (web != null && listName != "")
            {
                SPPictureLibrary list = web.Lists[listName] as SPPictureLibrary;
                if (list != null)
                {
                    foreach (SPListItem item in list.Items)
                    {

                        string filename = web.Url + "/" + SPUtility.GetUrlDirectory(item.File.Url) + "/_t/" + SPUtility.GetUrlFileName(item.File.Url.Replace('.','_') + ".jpg");

                        if (item.Title != "")
                            drpdwnLists.Items.Add(new ListItem(item.Title, filename));
                        else
                            drpdwnLists.Items.Add(new ListItem(item.Name, filename));
                    }
                }
            }


            //SPWeb web = SPContext.Current.Web;
            //foreach (SPList list in web.Lists)
            //    drpdwnLists.Items.Add(list.Title);
        }
    }
}
